package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.service.ChapterService;
import io.swagger.annotations.Api;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-10-26
 */
@RestController
@CrossOrigin
@Api(tags = "课时管理")
@RequestMapping("/admin/edu/chapter")
public class AdminChapterController {

    @Autowired
    ChapterService chapterService;

    @PostMapping("/save")
    public R save(@RequestBody Chapter chapter){
        boolean save = chapterService.save(chapter);
        if(save){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }
    }

    @GetMapping("/getById/{chapterId}")
    public R getById(@PathVariable String chapterId){
        Chapter chapter = chapterService.getById(chapterId);
        if (chapter != null) {
            return R.ok().data("item",chapter);
        }else{
            return R.error().message("数据不存在");
        }
    }

    @PutMapping("/update")
    public R update(@RequestBody Chapter chapter){
        boolean result = chapterService.updateById(chapter);
        if(result){
            return R.ok().message("更新成功");
        }else{
            return R.error().message("更新失败");
        }
    }

    @DeleteMapping("/delete/{chapterId}")
    public R delete(@PathVariable String chapterId){
        boolean result = chapterService.removeById(chapterId);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @GetMapping("/getChapterAndVideo/{courseId}")
    public R getChapterAndVideo(@PathVariable String courseId){
        List<Chapter> chapters = chapterService.getChapterAndVideo(courseId);
        return R.ok().data("items",chapters);
    }
}

